package com.lxw.redis.study.config;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisTemplateConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        FastJsonRedisSerializer<Object> fastJsonSerializer = new FastJsonRedisSerializer<>(Object.class);
        redisTemplate.setDefaultSerializer(fastJsonSerializer);
        //key序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //hash的key序列化方式
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        //value序列化方式
        redisTemplate.setValueSerializer(fastJsonSerializer);
        //hash的value序列化方式
        redisTemplate.setHashValueSerializer(fastJsonSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}
